﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace wp7CattoFreeAppv2
{
    public partial class ccattoTwitter : PhoneApplicationPage
    {
        public ccattoTwitter()
        {
            InitializeComponent();
            SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;

        }

        #region "Events"

        /// <summary> Btn to return to the Main Menu
        /// Btn to return to the Main Menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMainMenu_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/CattoMainMenu.xaml", UriKind.Relative));
        }

        /// <summary> - Lookup Button Click event
        /// Btn Lookup
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLookup_Click(object sender, RoutedEventArgs e)
        {
            WebClient twitter = new WebClient();

            twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
            twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + txtBoxLookup.Text));
                
        }

        #endregion

#region "Methods"

        void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e )
        {
            //throw new NotImplementedException();
            if (e.Error != null)

                return;

            XElement xmlTweets = XElement.Parse(e.Result);

            lstboxTwitter.ItemsSource = from tweet in xmlTweets.Descendants("status")
                                        select new K8TwitterItem
                                        { 
                                            ImageSource = tweet.Element("user").Element("profile_image_url").Value,
                                        Message = tweet.Element("text").Value,
                                            UserName = tweet.Element("user").Element("screen_name").Value
                                        
                                        };

        }
#endregion


    }

    /// <summary> - K8 Twitter Item Class
    /// K8 Twitter Item Class
    /// </summary>
    public class K8TwitterItem
    {
        public string UserName { get; set; }
        public string Message { get; set; }
        public string ImageSource { get; set; }


    }
}